package org.suburban.controller;
import java.util.Map;
import java.util.HashMap;
/**
 * Class to translate incoming client messages
 * @author Alexey Palamarchuk (slonnik@gmail.com)
 */
final class ClientCommandTranslator{

	/**
	 * Enumeration for all possible client command types
	 * @author Alexey Palamarchuk (slonnik@gmail.com)
	 *
	 */
	enum ClientCommandType
	{
		/** error*/
		ERROR,
		/** help*/
		HELP,
		/** generate up-to-time schedule*/
		UPTOTIME_SCHEDULE,
		/** generate up-to-time schedule (reverse mode)*/
		UPTOTIME_SCHEDULE_REVERSE,
		/** generate full-day schedule*/
		FULL_SCHEDULE,
		/** generate full-day schedule (reverse mode)*/
		FULL_SCHEDULE_REVERSE
	}
	
	interface ClientCommandArgument
	{
		
	}
	
	/**
	 * Class to describe translated 'client' command
	 * @author Alexey Palamarchuk (slonnik@gmail.com)
	 */
	static class ClientCommand
	{
		
		/** command type*/
		private final ClientCommandType type;
		/** command data*/
		private final Map<String, String> data;
		/**
		 * ctor
		 * @param type - command type
		 */
		ClientCommand(ClientCommandType type)
		{
			this.type = type;
			this.data = new HashMap<String, String>();
		}
		
		/**
		 * ctor
		 * @param type - command type
		 * @param data - command data
		 */
		ClientCommand(ClientCommandType type, Map<String, String> data)
		{
			this.type = type;
			this.data = data;
		}
		/**
		 * Get command type
		 * @return - command type
		 */
		ClientCommandType type()
		{
			return type;
		}
		
		/**
		 * Get command argument
		 * @param key - argument name
		 * @return argument value if it exists, otherwise NULL
		 */
		String argument(String key)
		{
			return data.get(key);
		}
		
		/**
		 * Check if command contains argument
		 * @param key - argument name
		 * @return true if command does contain argument, otherwise false
		 */
		boolean contains(String key)
		{
			return data.containsKey(key);
		}
	}
	
	/**
	 * Method to translate incoming 'client' message
	 * @param text - command text
	 * @return - translated client command
	 */
	public static ClientCommand translate(String text) {
			
		if(text.equalsIgnoreCase("?"))
		{
			/** return help command*/
			return new ClientCommand(ClientCommandType.HELP);
		}
		if(text.equalsIgnoreCase(">"))
		{
			/** return get 'up-to-time' schedule command*/
			return new ClientCommand(ClientCommandType.UPTOTIME_SCHEDULE);
		}
		
		if(text.equalsIgnoreCase("<"))
		{
			/** return get 'up-to-time' schedule command in reverse mode*/
			return new ClientCommand(ClientCommandType.UPTOTIME_SCHEDULE_REVERSE);
		}
		
		if(text.startsWith("@:"))
		{
			String[] params = text.split("@:");
			if(params.length == 1)
			{
				return new ClientCommand(ClientCommandType.FULL_SCHEDULE);
			}
			String arg = params[1].trim();
			if(arg.equalsIgnoreCase(">"))
			{
				return new ClientCommand(ClientCommandType.FULL_SCHEDULE);
			}
			if(arg.equalsIgnoreCase("<"))
			{
				return new ClientCommand(ClientCommandType.FULL_SCHEDULE_REVERSE);
			}
		}
		/** Empty commands are not allowed. */
		return new ClientCommand(ClientCommandType.ERROR);
		
	}
}
